package com.hzcx.app.simplechat.ui.publicui;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMMessage;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.chat.ChatUtil;
import com.hzcx.app.simplechat.chat.constant.ChatConstant;
import com.hzcx.app.simplechat.event.ForwardEvent;
import com.hzcx.app.simplechat.event.VideoMessageEvent;
import com.hzcx.app.simplechat.ui.chat.ChatShareUserActivity;
import com.hzcx.app.simplechat.ui.chat.event.ChatShareUserEvent;
import com.hzcx.app.simplechat.ui.publicui.interfaces.OnPublicMenuOnClickListener;
import com.hzcx.app.simplechat.util.down.DownLoadUtil;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.log.LogUtils;
import com.hzcx.app.simplechat.util.toast.ToastUtils;
import com.hzcx.app.simplechat.view.MyVideoView;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class VideoPalyActivity extends BaseActivity {
    public static final String VIDEO_URL = "url";
    @BindView(R.id.video_player)
    MyVideoView videoPlayer;
    OrientationUtils orientationUtils;
    private String source1;
    private EMMessage message;
    @Override
    public int getLayout() {
        return R.layout.activity_video_paly;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        videoPlayer =  this.findViewById(R.id.video_player);
        source1 = getIntent().getStringExtra(VIDEO_URL);
        videoPlayer.setUp(source1, true, "");

        //增加封面
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        videoPlayer.setThumbImageView(imageView);videoPlayer.setLongClickable(true);
        //增加title
        videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
        //设置返回键
        videoPlayer.getBackButton().setVisibility(View.VISIBLE);
        videoPlayer.getFullscreenButton().setVisibility(View.GONE);
        //设置旋转
        orientationUtils = new OrientationUtils(this, videoPlayer);
//        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
//        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                orientationUtils.resolveByClick();
//            }
//        });
        //是否可以滑动调整
        videoPlayer.setIsTouchWiget(true);
        //设置返回按键功能
        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        videoPlayer.startPlayLogic();
        videoPlayer.setOnLongPressListener(new MyVideoView.OnLongPressListener() {
            @Override
            public void onLongPress() {
                saveVideoToPhoto();
            }
        });
    }
    private void saveVideoToPhoto() {
        List<String> list = new ArrayList<>();
        list.add("发送给朋友");
        list.add("保存到相册");
        PublicRoundDialog roundDialog = new PublicRoundDialog(this, list, new OnPublicMenuOnClickListener() {
            @Override
            public void menuOnClick(int pos, String menu) {
                switch (menu) {
                    case "发送给朋友":
                        startActivity(new Intent(VideoPalyActivity.this, ChatShareUserActivity.class)
                                .putExtra(ChatShareUserActivity.INTENT_FROM_TAG, 5));
                        break;
                    case "保存到相册":
                        if (!TextUtils.isEmpty(source1)){
                            showLoading();
                            DownLoadUtil.downLoadVideo(source1, new DownLoadUtil.OnDownLoadListener() {
                                @Override
                                public void success(String path) {
                                    hideLoading();
                                    ToastUtils.show("保存成功");
                                }

                                @Override
                                public void fail() {
                                    hideLoading();
                                    ToastUtils.show("保存失败");
                                }
                            });
                        }
                        break;
                }
            }
        });
        roundDialog.show();
    }
    @Override
    public void initData() {

    }

    @Override
    protected void onPause() {
        super.onPause();
        videoPlayer.onVideoPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        videoPlayer.onVideoResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }

    @Override
    public void onBackPressed() {
        //先返回正常状态
        if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            videoPlayer.getFullscreenButton().performClick();
            return;
        }
        //释放所有
        videoPlayer.setVideoAllCallBack(null);
        super.onBackPressed();
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void getMessage(VideoMessageEvent event) {
        message = event.message;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void selectUserResult(ChatShareUserEvent event) {
        if (event.getTag() == 5) {
                // 消息列表单独转发
                List<EMMessage> messages = new ArrayList<>();
                messages.add(message);
                ChatUtil.getInstance().forWardBySelect(VideoPalyActivity.this, messages, event.getBean().getHxusername(), ChatConstant.CHATTYPE_SINGLE
                        , event.getBean().getMember_id(), EmptyUtils.isNotEmpty(event.getBean().getRemarks_name()) ? event.getBean().getRemarks_name() :
                                event.getBean().getNickname(), messageStatusCallback);
            showError("发送成功");
        }
    }

    protected EMCallBack messageStatusCallback = new EMCallBack() {
        @Override
        public void onSuccess() {
            LogUtils.d("发送成功");
            EventBus.getDefault().post(new ForwardEvent(0));
        }

        @Override
        public void onError(int i, String s) {
            LogUtils.d("发送失败：" + i + "   msg:" + s);
            EventBus.getDefault().post(new ForwardEvent(i));
        }

        @Override
        public void onProgress(int i, String s) {
            LogUtils.d("发送中：" + i + "   msg:" + s);
            EventBus.getDefault().post(new ForwardEvent(0));
        }
    };
}
